package com.jiuzhi.client.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MinaShortClientHandler extends IoHandlerAdapter{

    private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());

 

    public MinaShortClientHandler() {

      

    }

 

    @Override

    public void sessionOpened(IoSession session) {
    	logger.info("-IoSession实例:" + session.toString());  
    	  // 设置IoSession闲置时间，参数单位是秒  
    	  session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 13);  	
    }

 

    @Override

    public void messageReceived(IoSession session, Object message) {
       logger.info("客户端收到信息为：------> is:" + message.toString());
    }

 

    @Override

    public void exceptionCaught(IoSession session, Throwable cause) {

       session.close(true);

    } 
    
    /** 
     * session闲置的时候调用 
     */  
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
    	 if (status == IdleStatus.BOTH_IDLE)  
    	 {   // 如果IoSession闲置，则关闭连接  
    	  session.write("0x11");
    	 }
    }
}
